import { UserConfigFn, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import eslint from 'vite-plugin-eslint'
import legacy from '@vitejs/plugin-legacy'
import requireTransform from 'vite-plugin-require-transform'

const config = {
  envDir: 'config',
}

export default (function ({ mode }) {
  const env = loadEnv(mode, config.envDir)

  return {
    base: env.VITE_BASE_URL,
    plugins: [
      react(),
      requireTransform(),
      eslint({
        include: ['src/**/*.ts', 'src/**/*.tsx'],
        // fix: true,
      }),
      legacy({
        targets: ['defaults'],
      }),
    ],
    envDir: config.envDir,
    test: {
      environment: 'jsdom',
      include: ['test/**/*.{test,spec}.ts'],
    },
    resolve: {
      alias: [
        {
          find: /@\//,
          replacement: '/src/',
        },
      ],
    },
    css: {
      modules: {
        // 强制css modules使用驼峰
        localsConvention: 'camelCaseOnly',
      },
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "@/asset/css/common/_var.scss"as *;
          `,
        },
      },
    },
  }
} as UserConfigFn)
